package com.summer.framework.common.util;

import java.util.HashMap;
import java.util.Map;

/**
 * @auther: 杨杰
 * @date: 2019/1/14 11:15
 * @description:
 */
public class BaseContext {

    public static ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal();

    public BaseContext() {
    }

    public static void set(String key, Object value) {
        Map<String, Object> map = (Map) threadLocal.get();
        if (map == null) {
            map = new HashMap();
            threadLocal.set(map);
        }

        ((Map) map).put(key, value);
    }

    public static Object get(String key) {
        Map<String, Object> map = (Map) threadLocal.get();
        if (map == null) {
            map = new HashMap();
            threadLocal.set(map);
        }

        return ((Map) map).get(key);
    }

    private static String returnObjectValue(Object value) {
        return value == null ? null : value.toString();
    }

    public static void remove() {
        threadLocal.remove();
    }


    public static String getUserName() {
        return returnObjectValue(get("userName"));
    }

    public static void setUserName(String userName) {
        set("userName", userName);
    }

    public static String getUserId() {
        return returnObjectValue(get("userId"));
    }

    public static void setUserId(String userId) {
        set("userId", userId);
    }

/*    public String getHost() {
        return returnObjectValue(get("host"));
    }

    public void setHost(String host) {
        set("host",host);
    }*/
}
